package pri.zey.custom.activity;/**
 * Created by Administrator on 2016/3/12.
 */

import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * fragment管理类
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class FManager{

    private Stack<FragmentBase> mFragmentStack = new Stack<FragmentBase>();
//    public Message mMsg = new Message();

    //发送信息给Activity
    public void sendMsgToActivity(){

    }

    //发送信息给Fragment
    public void sendMsgToFragment(){

    }

    public Stack<FragmentBase> getmFragmentStack(){
        if(mFragmentStack != null) {
            return mFragmentStack;
        }else {
            return null;
        }
    }

    //添加fragment到stack
    public boolean addFragment(FragmentBase fragment){
        if(!isExist(fragment)) {
            mFragmentStack.add(fragment);
            return true;
        }else{
            //fragment已存在在stack中
            Log.i("TAGSS", "addFragment Method：" + fragment.getTag() + "已存在在stack中");
            return false;
        }
    }


    //删除stack中的fragment
    public void delFragment(FragmentBase fragment){
        for (FragmentBase fragmentBase : mFragmentStack){
            if(fragmentBase.equals(fragment)){
                mFragmentStack.remove(fragment);
            }
        }
    }

    //插入fragment到stact（把包空position后的元素删除）
    public boolean updateAndDelFragment(int position, FragmentBase fragment){
        boolean isState = false;
        int mStackSize = getStackSize();
        if(position < getStackSize()){
            int index = mStackSize - position;
            //保存包空position位置之后的元素存放到list中，并删除stack中的position位置元素
            while(index-- >= 0){
                mFragmentStack.remove(position);
            }
            //把插入fragment插入到stack中
            mFragmentStack.add(fragment);
            isState = true;
        }else{
            //越界无法插入
            isState = false;
        }
        return isState;
    }

    //插入fragment到stact（把包空position后的元素,存储到stack堆栈后面）
    public boolean updateAndSaveFragment(int position, FragmentBase fragment){
        boolean isState = false;
        int mStackSize = getStackSize();
        List<FragmentBase> list = new ArrayList<FragmentBase>();
        if(getStackSize() < getStackSize()){
            int index = mStackSize - position;
            //保存包空position位置之后的元素存放到list中，并删除stack中的position位置元素
            while(index-- >= 0){
                list.add(mFragmentStack.get(position));
                mFragmentStack.remove(position);
            }
            //把插入fragment插入到stack中
            mFragmentStack.add(fragment);
            //把备份到list中的元素，在存放回来
            for (FragmentBase fragmentBase : list){
                mFragmentStack.add(fragmentBase);
            }
            isState = true;
        }else{
            //越界无法插入
            isState = false;
        }
        return isState;
    }

    public int getStackSize(){
        return mFragmentStack.size() - 1;
    }

    //判断fragment是否存在
    public boolean isExist(FragmentBase fragment){
        for (FragmentBase fragmentBase : mFragmentStack){
            if(fragmentBase.equals(fragment)){
                return true;
            }
        }
        return false;
    }

    //获得fragment的位置
    public int getFragmentNumber(FragmentBase fragment){
        for (int i = getStackSize(); i > 0; i--){
            if(mFragmentStack.get(i).equals(fragment)){
                return i;
            }
        }
        return -1;
    }

    //返回顶部fragment
    public FragmentBase getTopFragment(){
        return mFragmentStack.get(getStackSize());
    }

    //返回指定fragment
    public FragmentBase getFragment(int position){
        if(getStackSize() <= position){
            return mFragmentStack.get(position);
        }else{
            //越界返回空
            return null;
        }
    }

    //返回第二个Fragment
    public FragmentBase getSecondFragment(){
        return mFragmentStack.get(getStackSize() - 1);
    }

    //返回指定fragment的下一个fragment
    public FragmentBase getNextFragment(FragmentBase fragment){
        int index = getFragmentNumber(fragment);
        if(index == -1){
            return mFragmentStack.get(0);
        }
        return mFragmentStack.get(index - 1);
    }
}
